import BaseService from '../BaseService'
import factory from '../serviceFactory'
import store from '@/store'
import * as optAct from '@/store/modules/options/MutationTypes'
/**
 * 专门用于获取下拉框选项
 */
class OptionsService extends BaseService {
  /* 获取部门选项 */
  getDepartment = factory.makeGet('/api/department/getDepartmentOptions', {failedW: false})

  /* 获取组织选项 */
  getGroup = factory.makeGet('/api/group/queryGroupList', {failedW: false})

  /* 获取我所属组织选项 */
  getMyGroup = factory.makeGet('/api/group/queryMyGroupList', {failedW: false})

  /* 获取职务选项 */
  getPositionLevel = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'POSITION_LEVEL'}, failedW: false})

  /* 获取审批选项 */
  getAuditType = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'AUDIT_TYPE'}, failedW: false})

  /** 获取 印章名称 **/
  getSealType = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'SEAL_TYPE'}, failedW: false})

  /* 获取请假类别选项 */
  getVacationType = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'LEAVE_TYPE'}, failedW: false})

  /* 获取请假类别选项 */
  getOvertimeType = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'OVERTIME_TYPE'}, failedW: false})

  /* 获取交通工具类别选项 */
  getTransportation = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'TRANSPORTATION'}, failedW: false})

  /** 获取其他申请类别选项 **/
  getCommonApplyType = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'COMMON_APPLY_TYPE'}, failedW: false})
  getOrderStatus = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'ORDER_STATUS'}, failedW: false})
  getShippingStatus = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'SHIPPING_STATUS'}, failedW: false})

  /** 获取 资源类型 **/
  getPlateNo = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'PLATE_NO'}, failedW: false})

  /** 获取 车牌号 **/
  getResourceType = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'RESOURCE_TYPE'}, failedW: false})

  /** 获取 资源 审批流程 最终审批人信息**/
  getFinalApproverUser = factory.makeGet('/api/apply/getFinalApproverUsers', {failedW: false})

  /* 获取打卡ID */
  getKqUser = factory.makeGet('/api/user/queryKqUserList', {failedW: false})

  /* 获取角色选项 */
  getRoleName = factory.makeGet('/api/role/queryRoleList', {failedW: false})

  /* 获取所有用户 */
  getUserList = factory.makeGet('/api/user/queryUserList', {failedW: false})

  /* 获取所有用户分组 */
  getUserTree = factory.makeGet('/api/attendance/queryAttendanceTree', {failedW: false})

  /** 获取资源管理类别选项 **/
  getResourceManageType = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'RESOURCE_MANAGE_TYPE'}, failedW: false})

  /** 获取航司选项 **/
  getAirLineOption = factory.makeGet('/api/airIssueRule/loadAirLineOption')

  /** 获取城市选项 **/
  getCityOption = factory.makeGet('/api/airIssueRule/loadCityOption')

  /* 获取支付科目 */
  getPayItem = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'PAY_ITEM'}, failedW: false})

  /* 获取易宝账户类型 */
  getYeePayAccountType = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'YEEPAY_ACCOUNT_TYPE'}, failedW: false})

  /**
   * 获取支付账号
   */
  getB2bPayAccount = factory.makeGet('/api/airB2bIssueAccount/loadB2bPayAccountOption')

  /* 获取网店平台 */
  getPlatform = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'PT_NAME'}, failedW: false})

  /* 获取采购方式 */
  getSupply = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'SUPPLY'}, failedW: false})

  /* 获取电话 */
  getCtcm = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'CTCM'}, failedW: false})

  /* 获取电话 */
  getCtct = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'CTCT'}, failedW: false})

  /* 获取导入类型 */
  getImportType = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'IMPORT_TYPE'}, failedW: false})

  /* 获取出票渠道 */
  getIssueChannel = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'ISSUE_CHANNEL'}, failedW: false})

  /* 获取证件类型 */
  getIdType = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'ID_TYPE'}, failedW: false})

  /* 获取退票类型 */
  getRefundType = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'REFUND_TYPE'}, failedW: false})

  /* 获取退票拒绝原因 */
  getRejectType = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'REJECT_TYPE'}, failedW: false})

  /* 获取支付方式 */
  getPayType = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'PAY_TYPE'}, failedW: false})

  /* 获取支付方式 */
  getPolicyCodeMark = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'POLICY_CODE_MARK'}, failedW: false})

  /* 获取退票描述 */
  getRefundDesc = factory.makeGet('/api/dict/loadDictDataByType', {extra: {type: 'REFUND_DESC'}, failedW: false})

  /**
   * 获取远程下拉选项
   * @param option  选项
   * @param update  是否强制更新
   */
  setOptions (option, update = false) {
    const _ = this.$lodash
    const options = store.state.options.options
    const setOption = obj => store.dispatch('options/' + optAct.SET_OPTION, obj)
    switch (option) {
      case 'MyGroup':
        if (_.isEmpty(options.MyGroup) || update) {
          this.getMyGroup().then(res => {
            setOption({MyGroup: res.data.map(item => { return {name: item.name, value: item.id} })})
          })
        }
        break
      case 'KqUser':
        if (_.isEmpty(options.KqUser) || update) {
          /* this.getKqUser().then(res => {
            setOption({KqUser: res.data.map(item => { return {name: item['dwname'] + '(' + item['dwenrollnumber'] + ')', value: item['dwenrollnumber']} })})
          }) */
        }
        break
      case 'RoleName':
        if (_.isEmpty(options.RoleName) || update) {
          this.getRoleName().then(res => {
            setOption({RoleName: res.data.map(item => { return {name: item['roleName'], value: item['id']} })})
          })
        }
        break
      case 'FinalApproverUser':
        if (_.isEmpty(options.FinalApproverUser) || update) {
          this.getFinalApproverUser().then(res => {
            setOption({FinalApproverUser: res.data.map(item => { return {name: item['name'], value: item['id']} })})
          })
        }
        break
      case 'UserList':
        if (_.isEmpty(options.UserList) || update) {
          this.getUserList().then(res => {
            setOption({UserList: res.data.map(item => { return {name: item['realName'], value: item['id']} })})
          })
        }
        break
      case 'UserTree':
        if (_.isEmpty(options.UserTree) || update) {
          this.getUserTree().then(res => {
            setOption({UserTree: res.data[0].nodes})
          })
        }
        break
      case 'Transportation':
        if (_.isEmpty(options.Transportation) || update) {
          this.getTransportation().then(res => {
            setOption({Transportation: res.data.map(item => { return {name: item['value'], value: item['value']} })})
          })
        }
        break
      default:
        if (_.isEmpty(options[option]) || update) {
          this['get' + option]().then(res => {
            const obj = {}
            obj[option] = res.data.map(item => { return {name: item.value, value: item.key} })
            setOption(obj)
          })
        }
        break
    }
  }

  clearOptions (option) {
    const obj = {}
    obj[option] = []
    store.dispatch('options/' + optAct.SET_OPTION, obj)
  }
}

export default new OptionsService()
